<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>

<script src="common.js" ></script>
<script src="chat.js" ></script>
<script src="message.js" ></script>

<script>
    let webSocket;
    function connectWeb(){
        let userId = document.getElementById("userId").value;
        if(null == userId || "" == userId || undefined == userId){
            alert("请填写用户名");
            return;
        }
        webSocket = connect(userId);
    }

    function bind(userId){

        let requestBody = new proto.RequestBody();
        requestBody.setUserid(userId);
        requestBody.setOptype("bind");
        requestBody.setDeviceid(generateUUID());
        requestBody.setTimestamp(new Date().getTime());
        if (webSocket.readyState != webSocket.OPEN){
            alert("通道被关闭了，请刷新页面");
            return;
        }
        console.log("发送绑定用户消息："+JSON.stringify(requestBody));


        let data = requestBody.serializeBinary();
        let protobuf = new Uint8Array(data.length);
        protobuf.set(data, 0);
        webSocket.send(protobuf);
        // webSocket.send(requestBody);

    }

    function appendMsg(msg){
        document.getElementById("my-msg").append(msg+'\r\n');
    }


</script>


<form id="my-form" onsubmit="return false">

    用户id: <input id="userId" type="text"/></br>
    <input type="button" value="连接websocket" onclick="connectWeb()"></br>


    <textarea id="my-msg"></textarea>


</form>
</body>
</html>